package cn.homework.dao.impl;

import cn.homework.dao.CommodityDao;
import cn.homework.domain.Product;
import cn.homework.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ACT
 */
public class CommodityDaoImpl implements CommodityDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public void addCommodity(Product product) {
        String sql = "insert into product values(?, ?, ?, ?, ?, ?, ?, ?)";
        template.update(sql, product.getId(), product.getType(), product.getName(), product.getPrice(),
                product.getQuantity(), product.getImage(), product.getDescription(), currentTime());
    }

    @Override
    public List<Product> findAllCommodity() {
        String sql = "select * from product";
        List<Product> list = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return list;
    }

    @Override
    public void delCommodityById(String id) {
        String sql = "delete from product where id = ?";
        template.update(sql, id);
    }

    @Override
    public Product findCommodityById(String id) {
        String sql = "select * from product where id = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Product.class), id);
    }

    @Override
    public void updateCommodity(String id, Product product) {
        String sql = "update product set type = ?, name = ?, price = ?, quantity = ?, image = ?, description = ?, time = ? where id = ?";
        template.update(sql, product.getType(), product.getName(), product.getPrice(), product.getQuantity(),
                product.getImage(), product.getDescription(), product.getTime(), id);;
    }

    @Override
    public List<Product> findCommodityCondition(Map<String, String[]> map) {
        String sql = "select * from product  where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        //遍历map
        Set<String> keySet = map.keySet();
        //定义参数的集合
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            //排除条件信息
            if ("demand".equals(key)) {
                continue;
            }

            //获取values
            String values = map.get(key)[0];
            //判断是否有值
            if (values != null && !"".equals(values)) {
                //有值
                sb.append(" and " + key + " like ? ");
                params.add("%" + values + "%");
            }
        }
        sb.append(" limit ? , ? ");
        params.add(0);
        params.add(8);
        sql = sb.toString();
        return template.query(sql, new BeanPropertyRowMapper<Product>(Product.class), params.toArray());
    }

    private String currentTime() {
        Date data = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String string = sdf.format(data);
        return string;
    }
}
